﻿/*----------------------------------------------------------------
// Copyright (C) 2011 
// 版权所有。 
//
// 文件名：Form1.cs
// 文件功能描述：
//
// 
// 创建标识：kenter 2011-10-19
// EMAIL:kenter1643@163.com
//
// 修改标识：
// 修改描述：
//
// 修改标识：
// 修改描述：
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Web;
using OAuth;
using WeiBOAPI.Common;
using WeiBOAPI.Sina;
using WeiBOAPI;
using WeiBoUI.FormEx;
using WeiBoUI.Helper;
using System.Drawing.Drawing2D;
using NLog;

namespace KKWeiBO
{
    [System.Runtime.InteropServices.ComVisibleAttribute(true)]
    public partial class MainFrm : FormBase
    {
        private static Logger logger = LogManager.GetCurrentClassLogger(); 
        PicShow picShow;
        UserLoginFrm _frm_userLogin = new UserLoginFrm();

        public MainFrm()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Global.Instance._g_SinaService._ac_Show += new Action<string>(AcShowImpl);
            Global.Instance._g_SinaService._ac_LoginIn += new Action<SinaUserInfo>(AcLoginInImpl);

            webBrowser1.ScriptErrorsSuppressed = true; //禁用错误脚本提示
            webBrowser1.IsWebBrowserContextMenuEnabled = false; //禁用右键菜单
            webBrowser1.WebBrowserShortcutsEnabled = false; //禁用快捷键
            webBrowser1.ObjectForScripting = this;

            Global.Instance._g_SinaService.GetEmotions();

            this.Hide();
            _frm_userLogin.ShowDialog();
            this.Show();

            Global.Instance._g_SinaService.UserShow(Global.Instance._g_SinaService._user_Id);
        }

        public void AcShowImpl(string msg)
        {
            BeginInvoke((MethodInvoker)delegate
            {
                this.webBrowser1.DocumentText = msg;
            });
        }

        public void AcLoginInImpl(SinaUserInfo user)
        {
            BeginInvoke((MethodInvoker)delegate
            {
                try
                {
                    this.pbUserImg.Load(user.HeadImage);
                }
                catch (System.InvalidOperationException ex)
                {
                    logger.Error(ex.Message);
                }
                catch (System.ArgumentException ex)
                {
                    logger.Error(ex.Message);
                }
                catch (System.IO.FileNotFoundException ex)
                {
                    logger.Error(ex.Message);
                }

                try
                {
                    this.lbUserName.Text = user.ScreenName;
                    lbl_fs.Text = "粉丝(" + user.FollowersCount + ")";
                    lbl_wb.Text = "微博(" + user.StatusesCount + ")";
                    lbl_gz.Text = "关注(" + user.FriendsCount + ")";
                }
                catch (System.Exception ex)
                {
                    logger.Error(ex.Message);
                }

                Global.Instance._g_SinaService.FriendsTimeLine();
                
            });
            
        }

        public void ShowPic(string imgUrl)
        {
            if (Application.OpenForms["PicShow"] == null)
            {
                picShow = new PicShow();
            }
            
            Point screenPoint = this.PointToScreen(new Point(0,0));
            if (screenPoint.X > picShow.Width)
            {
                picShow.Location = new Point(this.Location.X - picShow.Width, this.Location.Y);
            } 
            else
            {
                picShow.Location = new Point(this.Location.X + this.Width, this.Location.Y);
            }
            
            picShow.LoadPic(imgUrl);
        }

        private void pbUserImg_Click(object sender, EventArgs e)
        {
            CommonTools.OpenIE(@"http://weibo.com/" + Global.Instance._g_SinaUser.Id);
        }

        private void vsb_news_Scroll(object sender, ScrollEventArgs e)
        {
            //this.webBrowser1.Document.Window.ScrollTo(new Point(0, vsb_news.Value * 32));
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (this.webBrowser1.ReadyState == WebBrowserReadyState.Complete
                || this.webBrowser1.ReadyState == WebBrowserReadyState.Interactive)
            {

                //if (this.webBrowser1.Document.Body.ScrollRectangle.Height > this.webBrowser1.Height)
                //{
                //    vsb_news.Minimum = 0;
                //    vsb_news.Maximum = (this.webBrowser1.Document.Body.ScrollRectangle.Height
                //        - this.webBrowser1.Height) / 32;

                //    vsb_news.Show();
                //}
                //else
                //{
                //    vsb_news.Hide();
                //}
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            Global.Instance._g_SinaService.FriendsTimeLine();
        }

        private void MainFrm_SizeChanged(object sender, EventArgs e)
        {
            webBrowser1.Width = this.Width - 24;
            webBrowser1.Height = this.Height - 140;
            webBrowser1.Location = new Point(12, 90);
        }

        private void tsbtn_MyWeiBo_Click(object sender, EventArgs e)
        {
            Global.Instance._g_SinaService.GetMentions();
        }

        public void ShowUserNews(string userName)
        {
            Global.Instance._g_SinaService.GetUserNews(userName);
        }

        private void btn_Post_Click(object sender, EventArgs e)
        {
            PostNewFrm postNewFrm = new PostNewFrm();
            postNewFrm.StartPosition = FormStartPosition.CenterParent;
            postNewFrm.ShowDialog();
        }

        private void tsbtn_MyPost_Click(object sender, EventArgs e)
        {

        }
    }
}
